package com.example.demo.designPatternsTest;

import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

/**
 * @author lilinchun
 * @date 2023/5/22 0022 15:27
 */
@Slf4j
@RestController
@RequestMapping("/design")
@Api(tags = "设计模式")
public class DesignPatternsController {


    public static void main(String[] args) {
        //------------------------------策率模式--------------------
        //定义三个类型的对象
        System.out.println("***************策率模式************************");
        NormalPerson normalPerson = new NormalPerson();
        Vip1Person vip1Person = new Vip1Person();
        Vip2Person vip2Person = new Vip2Person();
        //new context类对象 将三个类型的对象传入
        PersonContext npersonContext = new PersonContext(normalPerson);
        PersonContext v1personContext = new PersonContext(vip1Person);
        PersonContext v2personContext = new PersonContext(vip2Person);
        //利用多态 通过调用context类对象的计算价格方法 实际上调用的子类的计算价格方法 得到最终价格
        System.out.println("普通会员: " + npersonContext.getPrice(300, 20));
        System.out.println("VIP1: " + v1personContext.getPrice(300, 20));
        System.out.println("VIP2: " + v2personContext.getPrice(300, 20));
        //---------------------------------------------------------

        //-----------------------------工厂模式----------------------
        System.out.println("***************工厂模式************************");
        Car car = new ProductFactory().getCar();
        Plane plane = new ProductFactory().getPlane();
        car.go();
        plane.go();
        //----------------------------------------------------------
        //----------------------------抽象工厂模式-------------------
        //生产五菱汽车
        System.out.println("***************抽象工厂模式************************");
        WuLingFactory wuLingFactory = new WuLingFactory();
        CarProduct carProduct = wuLingFactory.productCar();
        carProduct.start();
        carProduct.run();
        carProduct.shutDown();
        //生产特斯拉汽车
        TeslaFactory teslaFactory = new TeslaFactory();
        CarProduct carProduct1 = teslaFactory.productCar();
        carProduct1.start();
        carProduct1.run();
        carProduct1.shutDown();
        //-----------------------------------------------------------
        //------------------------------装饰模式----------------------
        //new 一个wuling敞篷类
        System.out.println("***************装饰模式************************");
        Wulingchangpeng wulingchangpeng = new Wulingchangpeng();
        //调用敞篷装饰器 增加敞篷功能
        NoDoorDecorate noDoorDecorate = new NoDoorDecorate(wulingchangpeng);
        noDoorDecorate.run();
        RunLongDecorate runLongDecorate1 = new RunLongDecorate(wulingchangpeng);
        runLongDecorate1.run();
        System.out.println("*****************");
        WulingGameBoy wulingGameBoy = new WulingGameBoy();
        RunLongDecorate runLongDecorate = new RunLongDecorate(wulingGameBoy);
        runLongDecorate.run();
        //--------------------------------------------------------------
        //----------------------------动态代理--------------------------
        System.out.println("***************动态代理************************");
        CarMoveProxy carMoveProxy = new CarMoveProxy();
        CarNew carNew = new CarNew();
        carMoveProxy.doRun(carNew, new InvocationHandler() {
            @Override
            public Object invoke(Object o, Method method, Object[] objects) throws Throwable {
                System.out.println("汽车启动...");
                Object invoke = method.invoke(carNew, objects);
                System.out.println("汽车关闭...");
                return invoke;
            }
        });
        //--------------------------------------------------------------

        //----------------------------适配器----------------------------
        System.out.println("***************适配器************************");
        PowerAdapter powerAdapter = new PowerAdapter(new TypeA());
        powerAdapter.v5();

        PowerAdapter powerAdapter2 = new PowerAdapter(new TypeB());
        powerAdapter2.v5();
        //-------------------------------------------------------------
    }


}
